using System;
using System.Collections.Generic;
using System.Text;
using Emlyn.Common.Data.DBUpdater;
using Emlyn.Common.Data.SqlServer.SqlManagers;

namespace Emlyn.Website.EmlynORegan.DBUpdateRules
{
    public class DUR_0060_tblEOArticle_Article: IDBUpdaterRule
    {
        #region IDBUpdaterRule Members

        public int UpgradeFromVersion()
        {
            return 50;
        }

        public int UpgradeToVersion()
        {
            return 60;
        }

        public void PerformUpgrade()
        {
            string ltblEOArticle_Article =
                "CREATE TABLE [dbo].[tblEOArticle_Article] ( " +
                "	[Article_ArticleID] [uniqueidentifier] NOT NULL , " +
                "	[ParentArticleID] [uniqueidentifier] NOT NULL , " +
                "	[ChildArticleID] [uniqueidentifier] NOT NULL  " +
                ") ";
            SqlManager.Execute(ltblEOArticle_Article);


            string ltblEOArticle_ArticlePK =
                "ALTER TABLE [dbo].[tblEOArticle_Article] WITH NOCHECK ADD  " +
                "	CONSTRAINT [PK_tblEOArticle_Article] PRIMARY KEY  CLUSTERED  " +
                "	( " +
                "		[Article_ArticleID] " +
                "	) ";
            SqlManager.Execute(ltblEOArticle_ArticlePK);

            string ltblEOArticle_Article_defaultid =
                "ALTER TABLE [dbo].[tblEOArticle_Article] ADD " +
                "CONSTRAINT [DF_tblEOArticle_Article_Article_ArticleID] " +
                "DEFAULT (newid()) " +
                "FOR [Article_ArticleID] ";
            SqlManager.Execute(ltblEOArticle_Article_defaultid);

            string lforeignKeys =
                "ALTER TABLE [dbo].[tblEOArticle_Article] ADD " +
                "CONSTRAINT [FK_tblEOArticle_Article_tblEOArticle] FOREIGN KEY  " +
                "(  " +
                "	[ParentArticleID] " +
                ") REFERENCES [dbo].[tblEOArticle] ( " +
                "	[ArticleID] " +
                "), " +
                "CONSTRAINT [FK_tblEOArticle_Article_tblEOArticle1] FOREIGN KEY  " +
                "( " +
                "	[ChildArticleID] " +
                ") REFERENCES [dbo].[tblEOArticle] ( " +
                "	[ArticleID] " +
                ") ON DELETE CASCADE  ON UPDATE CASCADE  ";
            SqlManager.Execute(lforeignKeys);
        }

        #endregion
    }
}
